<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC3015 – In POSIX sh, `=~` regex matching is undefined.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC3015 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC3015">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="in-posix-sh--regex-matching-is-undefined">In POSIX sh,
<code>=~</code> regex matching is undefined.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC3015.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb1-2"><a href="SC3015.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="bu">[</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span> =~ foo[0-9]+ <span class="bu">]</span><span class="kw">;</span> <span class="cf">then</span></span>
<span id="cb1-3"><a href="SC3015.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> matched</span>
<span id="cb1-4"><a href="SC3015.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC3015.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb2-2"><a href="SC3015.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Use the x-hack to handle strings like &#39;(&#39;.</span></span>
<span id="cb2-3"><a href="SC3015.html#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="fu">expr</span> <span class="st">&quot;X</span><span class="va">$var</span><span class="st">&quot;</span> : <span class="st">&#39;X.*foo[0-9]\{1,\}&#39;</span> <span class="op">&gt;</span>/dev/null<span class="kw">;</span> <span class="cf">then</span></span>
<span id="cb2-4"><a href="SC3015.html#cb2-4" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> matched</span>
<span id="cb2-5"><a href="SC3015.html#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC3015.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb3-2"><a href="SC3015.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="va">$var</span> <span class="kw">in</span></span>
<span id="cb3-3"><a href="SC3015.html#cb3-3" aria-hidden="true" tabindex="-1"></a>  <span class="pp">*</span><span class="ss">foo[0-9]</span><span class="pp">*</span><span class="kw">)</span></span>
<span id="cb3-4"><a href="SC3015.html#cb3-4" aria-hidden="true" tabindex="-1"></a>    <span class="bu">echo</span> matched</span>
<span id="cb3-5"><a href="SC3015.html#cb3-5" aria-hidden="true" tabindex="-1"></a>    <span class="cf">;;</span></span>
<span id="cb3-6"><a href="SC3015.html#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="cf">esac</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You are using <code>=~</code> in a script declared to be compatible
with POSIX sh or Dash, but <code>=~</code> is <a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html">not
specified by POSIX</a> and is unlikely to work outside
<code>[[ ]]</code> in Bash and Ksh.</p>
<p>Use <a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/expr.html#tag_20_42_13_01"><code>expr</code>'s
<code>:</code> operator</a> instead. It may be necessary to revise the
regular expression because POSIX <code>expr</code> uses <a
href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03">basic
regular expressions</a> anchored to the beginning of the string, as
opposed to the unanchored <a
href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04">extended
regular expressions</a> used by <code>[[ str =~ re ]]</code> in Bash and
Ksh.</p>
<p>Alternately, use <code>case</code> if the matching can be done with
<a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13">shell
patterns</a> instead of regular expressions. This avoids the need for an
external utility.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>None</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a href="https://mywiki.wooledge.org/BashFAQ/041">Greg's Wiki -
BashFAQ #41: How do I determine whether a variable contains a
substring?</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


